package com.tjvib.view.activity;

import static com.tjvib.Constants.SUCCESS;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

import com.tjvib.R;
import com.tjvib.base.BaseActivity;
import com.tjvib.presenter.BasePresenter;
import com.tjvib.util.LogUtil;
import com.tjvib.util.Manager.AppManager;
import com.tjvib.util.Manager.UserManager;
import com.tjvib.util.ToastUtil;
import com.tjvib.util.retrofit.BaseResponse;
import com.tjvib.util.retrofit.RetrofitHelper;
import com.tjvib.widget.AndroidScheduler;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class FeedbackActivity extends BaseActivity {

    private Button feed_btn_upload;
    private EditText feed_et_con;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_feedback;
    }

    @Override
    protected boolean initView() {
        feed_et_con = findViewById(R.id.feed_et_con);

        feed_btn_upload = findViewById(R.id.feed_btn_upload);
        feed_btn_upload.setOnClickListener(v -> {
            if (feed_et_con.getText().toString().isEmpty()) {
                ToastUtil.show("反馈内容不能为空");
                return;
            }
            RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"),
                    getFeedBack(feed_et_con.getText().toString()));
            RetrofitHelper.getInstance().create()
                    .feedback(requestBody, String.valueOf(UserManager.getInstance().getUserId()))
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidScheduler.mainThread())
                    .subscribe(new Observer<BaseResponse>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                        }

                        @Override
                        public void onNext(BaseResponse value) {
                            if (value.getCode() == SUCCESS) {
                                ToastUtil.show("提交成功");
                                AppManager.getInstance().finishActivity();
                            } else {
                                showErrorDialog("提交失败，请重试" + value.getMessage());
                                LogUtil.e(value.getMessage() + "\n" + value.getData());
                            }
                        }

                        @Override
                        public void onError(Throwable e) {
                            showErrorDialog("提交失败，请重试" + e.getMessage());
                        }

                        @Override
                        public void onComplete() {
                        }
                    });
        });

        return true;
    }

    @Override
    protected boolean initData() {
        return true;
    }

    @Override
    protected BasePresenter genPresenter() {
        return null;
    }

    private String getFeedBack(String content) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("description", content);
        map.put("userId", UserManager.getInstance().getUserId());

        LogUtil.e("getFeedBack: map json:" + new JSONObject(map).toString());
        return new JSONObject(map).toString();
    }
}